package 面试;

import java.util.PriorityQueue;

public class TOPK1 {

    public static void main(String[] args) {

        /**
         * 寻找该数组里面前 10 大的数
         */

        int[] a = {5,36,95,12,23,561,36,85,
                410,236,95,148,65,23,5,9,
                84,5,13,512,95,26,512,856,
                666,354,89,412,475,36,103,
                894,229,3285,225,3236,984,
                0,7,795,56,32,301,98,56,7456,
                21,985,311,460,59,96,63,98,510};

        // 创建小顶堆，初始化前 10 个元素
        PriorityQueue queue = new PriorityQueue(10);
        for (int i = 0; i < 10; i++) {
            queue.add(a[i]);
        }
        for (int i = 10; i < a.length; i++) {
            // 从数组的第 11 个元素开始与小顶堆堆顶元素比较
            if (a[i] > (int)queue.peek()) {
                // 挤掉堆顶元素，替换它并调整堆
                queue.poll();
                queue.add(a[i]);
            }

            // 小于等于就不用管，直接往下走
        }

        System.out.println(a.length == queue.size());

        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}
